/**
 * Created by hcadmin on 16/10/25.
 */
var num_model=0;
//获取模板信息
function initLogModelList(group_id,status,which_page){
    //which_page 0-首页 1-添加模板 2-管理 3-报表获取模板
    var url = apiurl+"/dailyrecords/temp/list";
    var data = {
        group_id:group_id,
        dailyrecord_temp_status:status
    };
    $.ajax({
        beforeSend:function(){
          $(".loading").show();
            $(".show_hide").hide();
            $(".choice_div_content").hide();     //日志报表页面  筛选页面
        },
        url:url,
        type:"GET",
        data:data,
        cache:false,
        async: true,
        success:function(d){
            //console.log(d);
            $(".loading").hide();
            $(".show_hide").show();
            $(".choice_div_content").show();
            var data = JSON.parse(d).data;
            var title_length_arr = [];
            var id_arr = [];

            for(var i=0;i<data.length;i++){
                var id          = data[i].dailyrecord_temp_id;          //id
                var status      = data[i].dailyrecord_temp_status;      //-1-全部,0-停用,1-启用
                var icon        = data[i].dailyrecord_temp_icon;        //icon
                var title       = data[i].dailyrecord_temp_title;       //title
                var sequence    = data[i].sequence;
                var user_id     = data[i].user_id;
                var group_id    = data[i].group_id;
                num_model++;

                if(which_page=="0"){
                    if(status=="1"){
                        var html = "<div class='col-xs-4 table_big' id='"+id+"'><div class='table_mid'><img class='table_small_cover' src='"+icon+"'> <div class='table_small_text'>"+title+"</div></div></div>";
                        $(".table_content").prepend(html);
                        title_length_arr.push(title.length);
                        id_arr.push(id);
                    }
                }else if(which_page=="1"){
                    var img_url = "";

                    if(status=="1"){
                        img_url = "../../approval/approvalImg/Giab/hadAdd.png";
                        $(".had_used").append("<div class='model_cell_big had_div'><div class='model_cell_mid'><div class='col-xs-8'><img src='"+icon+"' class='model_cover'><span class='model_name'>"+title+"</span></div><div class='col-xs-4'><img src='"+img_url+"' class='model_use_state' group_id='"+group_id+"' id='"+id+"' status='"+status+"'></div></div></div>");
                    }else{
                        img_url = "../logImg/add.png";
                        $(".un_used").append("<div class='model_cell_big un_div'><div class='model_cell_mid'><div class='col-xs-8'><img src='"+icon+"' class='model_cover'><span class='model_name'>"+title+"</span></div><div class='col-xs-4'><img src='"+img_url+"' class='model_use_state' group_id='"+group_id+"' id='"+id+"' status='"+status+"'></div></div></div>");
                    }
                }else if(which_page=="2"){
                    //管理
                    var html_ad=
                        "<div class='bigest_one'>" +
                            "<div class='model_cell_big'>" +
                                "<div class='model_cell_mid'>" +
                                    //"<div class='col-xs-7'>" +
                                        "<img src='"+icon+"' class='model_cover'>" +
                                        "<div class='model_name'>"+title+"</div>" +
                                    //"</div>" +
                                    //"<div class='col-xs-5' style='height: 100%;padding-left: 29vw'>" +
                                        "<img src='../logImg/threeDots.png' class='model_use_state'>" +
                                    //"</div>" +
                                "</div>" +
                            "</div>" +
                            "<div class='hidden_part'>" +
                                "<div class='col-xs-8'></div>" +
                                "<div class='col-xs-2 edit_log_model'>" +
                                    "<div class='hidden_part_icon hidden_part_icon_edit' icon='"+icon+"' user_id='"+user_id+"' group_id='"+group_id+"' sequence='"+sequence+"' dailyrecord_temp_id='"+id+"'>" +
                                        "<img src='../logImg/edit.png' class='hidden_part_icon_pic_edit'>" +
                                        "<br/>" +
                                        "编辑" +
                                    "</div>" +
                                "</div>" +
                                "<div class='col-xs-2 delete_log_model'>" +
                                    "<div class='hidden_part_icon hidden_part_icon_delete' dailyrecord_temp_id='"+id+"'>" +
                                        "<img src='../logImg/dustbin.png' dailyrecord_temp_id='"+id+"' class='hidden_part_icon_pic'>" +
                                        "<br/>" +
                                        "删除" +
                                    "</div>" +
                                "</div>" +
                            "</div>" +
                        "</div>";
                    $(".content_bigest").append(html_ad);
                }else{
                    var html_name = "<div class='model_body' data-id='"+id+"' data-choiced='0'>"+title+"</div>";
                    $(".choice_model_show").append(html_name);
                }
            }
            if(which_page=="0"){
                for(var a=1;a<100;){
                    if($(".table_big").length==a){
                        $(".table_content").append("<div class='blank_table_big col-xs-4'></div>");
                    }
                    a+=3;
                }
            }
            if ($(".table_big").length > 0) {
                var maxHeight = 0;
                $(".table_big").each(function(){
                    //debugger;
                    var currentHeight = $(this).height();
                    if(currentHeight > maxHeight){
                        maxHeight = currentHeight;
                    }
                }).css('height',maxHeight);

                $(".blank_table_big").css('height',maxHeight);

            }
            //if(title_length_arr.length>0){
            //    var j = title_length_arr[0];
            //    var maxId = id_arr[0];
            //    var maxHeight = title_length_arr[0];
            //    for(key in title_length_arr){
            //        if(title_length_arr[key]>j){
            //            j = title_length_arr[key];
            //            //maxIndex = key;
            //            maxId = id_arr[key];
            //        }
            //    }
            //
            //    $(".table_big").each(function(){
            //        if($(this).attr("id")==maxId){
            //            maxHeight = $(this).css('height');
            //        }
            //    }).css('height',maxHeight);
            //    $('.blank_table_big').css('height',maxHeight);
            //}

            if(which_page=="1"){
                $(".had_div").last().children(0).css("border","none");
                $(".un_div").last().children(0).css("border","none");
            }
            if(which_page=="2"){
                $(".model_cell_mid").last().css("border","none");
                //$(".model_cell_big").last().css("borderBottom","1px solid #e5e5e5");
                $(".bigest_one").last().children(1).css("borderBottom","1px solid #e5e5e5");
            }

            $('.table_small_cover').each(function(){
                var src = $(this).attr("src");
                if(src=="../logImg/location.png"){
                    $(this).css("width","9.11vw");
                }
            })
        }
    })
}

//获取日志列表
function initLogList(id,listType,user_id,is_read,from_id,dailyrecord_id){
    var data = {
        listType:listType,
        user_id:user_id,
        is_read:is_read,
        from_id:from_id,
        dailyrecord_id:dailyrecord_id,
        token:localStorage.token,
        group_id:localStorage.group_id
    };

    $.ajax({
        beforeSend:function(){
            $(".loading").show();
        },
        url:apiurl+"/dailyrecords/list",
        type:"GET",
        data:data,
        success:function(d){
            if(typeof d == "object"){
                var msg= d.msg;
                if(msg=="202") {
                    $(".token_iframe").fadeIn(1000);
                }
            }
            else
            {
                $(".loading").hide();
                var data     = JSON.parse(d).data.list;
                var userInfo = JSON.parse(d).data.userCallbackString;
                //console.log(data);
                if(data.length==0){
                    if(listType=="1"){
                        $(".blank_text").text("暂无发出日志");
                        $(".blank_log").show();
                    }else{
                        $(".blank_text").text("暂无收到日志");
                        $(".blank_log").show();
                    }
                }
                else{
                    $(".blank_log").hide();
                    for(var i=0;i<data.length;i++){
                        var user_id             = data[i].user_id;
                        var user_name;
                        var user_icon;
                        var user_qyxBindId;
                        for(var a=0;a<userInfo.length;a++){
                            if(userInfo[a].id==user_id){

                                user_name       = userInfo[a].name;
                                user_icon       = userInfo[a].avatar;
                                user_qyxBindId  = userInfo[a].qyxBindId
                            }
                        }
                        var avatar_img;
                        //if(user_icon=="") {
                            var color = avatar_getColor(user_qyxBindId);
                            avatar_img = "<div class='cover_pic_div' style='background-color: "+color+"'>"+avatar_name(user_name)+"</div>"
                        //}else{
                        if (user_icon.length > 0) {
                            var src = avatar_getUrl(localStorage.user_id,localStorage.token,user_icon);
                            avatar_img += "<img class='cover_pic' src='"+src+"' hidden>";
                        }
                        var dailyrecord_id      = data[i].dailyrecord_id;
                        var dailyrecord_img     = data[i].dailyrecord_img;
                        var is_read             = data[i].is_read;
                        var date                = new Date(data[i].create_timestamp);
                        var fieldValues         = data[i].fieldValues;
                        var title               = data[i].dailyrecord_temp_title;


                        initDate(date);

                        var timeSpan = "<span class='date_time'>"+day_min+"</span>";

                        //console.log(day_min);

                        var had_read            = "";
                        var num_file            = 0;
                        var file_div            = "";
                        if(dailyrecord_img.length>0){
                            var dailyrecord_img_arr = dailyrecord_img.split(",");
                            for(var j=0;j<dailyrecord_img_arr.length;j++){
                                num_file +=1;
                            }
                            file_div = "<div class='enclosure_pic'>"+num_file+"个附件</div>";
                        }else{
                            file_div = "";
                        }

                        var read_no = "<span class='state'>未读</span>";
                        //var read    = "<div style='width: 40vw;height: 100%;text-align: right;float: left'><span class='state' style='color: #B3B3B3'>已读</span></div>";
                        var read    = "<span class='state' style='color: #B3B3B3'>已读</span>";
                        if(is_read==0){
                            had_read = read_no;
                        }else{
                            had_read = read;
                        }

                        if(listType=="1"){
                            had_read = "<div style='height: 100%;width: 1px;background-color: white;border: none;margin-left: 13vw;float: left'></div>";
                        }

                        var title_arr   = [];
                        var title_str;
                        //var val_arr     = [];
                        var val_str;
                        var type;
                        var grey_title_html = "";
                        var black_content_html = "";

                        //console.log(fieldValues);

                        for(var z=0;z<fieldValues.length;z++){
                            type = fieldValues[z].daiyrecord_temp_field_type;
                            if(type=="3"){
                                var time1 = new Date(Math.round(fieldValues[z].default_value));
                                time1 = initDate(time1);
                                val_str = year_min;
                            }else if(type=="4"){
                                var time2  = new Date(Math.round(fieldValues[z].default_value));
                                time1 = initDate(time2);
                                val_str = year_mon_day;
                            }else{
                                val_str = fieldValues[z].default_value;
                            }
                            title_str = fieldValues[z].dailyrecord_temp_field_title;

                            grey_title_html += "<div class='content_div' style='color: #a1aab2'>"+title_str+"</div>";
                            black_content_html += "<div class='content_div' style='color: #333333'>"+val_str+"</div>";
                        }
                        //title_str=title_arr.join(",");
                        //val_str=val_arr.join(",");
                        var html = "<div class='tableview_cell_big' id='"+dailyrecord_id+"' user_id='"+user_id+"'>" +
                            "<div class='tableview_cell_first'>" +
                            "<div style='width: 65vw;height: 100%;float: left'>"+avatar_img+"<span class='name1'>"+user_name+"</span></div>" +
                            ""+had_read+"<div class='model_state'>"+title+"</div>" +
                            "</div>" +
                            "<div class='tableview_cell_second'>" +
                            "<div class='col-xs-4 content_big'>" +
                            "<div class='content_real' style='color: #B3B3B3'>"+grey_title_html+"" +
                            "</div>" +
                            "</div>" +
                            "<div class='col-xs-8 content_big'>" +
                            "<div class='content_real'>"+black_content_html+"" +
                            "</div>" +
                            "</div>" +
                            "</div>" +
                            "<div class='tableview_cell_third'>" +
                            timeSpan +
                            ""+file_div+"" +
                            "</div>" +
                            "</div>";
                        $("#"+id+"").append(html);
                    }
                }

                showAvatar(".cover_pic");

                if(is_read=="0"){
                    if(data.length!=0){
                        $(".blank_log").hide();
                    }else{
                        $(".blank_text").text("暂无未读日志");
                        $(".blank_log").show();
                    }
                }
            }


        }

    })
}

function showAvatar(classname){
    $(classname).one("load",function(){
        $(this).show();
        $(this).prev().hide();
    });
}